package org.jground.rx.ui.zk;

import org.jground.rx.RXAccess;
import org.jground.rx.IRXObject;
import org.jground.rx.ui.IWindowEditAction;
import org.jground.zk.zulX.HboxX;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zul.Button;

public class UIWindowEditCommandPanel extends HboxX implements EventListener {
    //

    private Button revertToSavedBt;
    private Button saveBt;
    private Button saveAndCloseBt;
    private Button closeBt;
    private Button deleteBt;
    private Button deletionConfirmBt;
    private Button deletionAbortBt;
    private UIFrame uiFrame;
    //
    // private UIDialogConfirm confirmDialog = new UIDialogConfirm();
    //

    private IWindowEditAction windowEditAction;

    //
    public UIWindowEditCommandPanel(UIFrame uiFrame, IWindowEditAction windowEditAction) {
        this.uiFrame = uiFrame;
        this.windowEditAction = windowEditAction;
        this.setSclass(UIStyles.BUTTONGROUP);
        //
        revertToSavedBt = new Button("Revert to Saved");
        saveBt = new Button("Save");
        saveAndCloseBt = new Button("Save and close");
        closeBt = new Button("Close");
        deleteBt = new Button("Delete");
        deletionConfirmBt = new Button("Yes, delete it");
        deletionAbortBt = new Button("No");
        //
        // add(revertToSavedBt, saveBt, saveAndCloseBt, closeBt, deleteBt);
        //
        revertToSavedBt.addEventListener(Events.ON_CLICK, this);
        saveBt.addEventListener(Events.ON_CLICK, this);
        saveAndCloseBt.addEventListener(Events.ON_CLICK, this);
        closeBt.addEventListener(Events.ON_CLICK, this);
        deleteBt.addEventListener(Events.ON_CLICK, this);
        deletionConfirmBt.addEventListener(Events.ON_CLICK, this);
        deletionAbortBt.addEventListener(Events.ON_CLICK, this);
        //
        // confirmDialog.SetButtons(deletionConfirmBt, deletionAbortBt);
    }

    public void onEvent(Event hEvent) {
        if (hEvent.getTarget() == revertToSavedBt) {
            windowEditAction.revertToSaved();
            return;
        }
        if (hEvent.getTarget() == saveBt) {
            windowEditAction.saveData();
            return;
        }
        if (hEvent.getTarget() == saveAndCloseBt) {
            windowEditAction.saveData();
            windowEditAction.close();
            return;
        }
        if (hEvent.getTarget() == closeBt || hEvent.getName().equals(Events.ON_CLOSE)) {
            windowEditAction.close();
            return;
        }
        if (hEvent.getTarget() == deleteBt) {
            windowEditAction.openConfirmDialog();
            return;
        }
        if (hEvent.getTarget() == deletionAbortBt) {
            windowEditAction.show();
            return;
        }
        if (hEvent.getTarget() == deletionConfirmBt) {
            windowEditAction.deleteData();
            uiFrame.showHomePanel();
            // editPanel.Close();
            return;
        }
    }

    // public UIDialogConfirm GetDialog(String confirmText, HComponent content)
    // {
    // this.confirmDialog.SetConfirmContent(confirmText, content);
    // return confirmDialog;
    // }

    public void setData(IRXObject moDataObject) {
        this.removeAll();
        add(revertToSavedBt);
        if (RXAccess.getInstance().canWrite(moDataObject)) {
            add(saveBt);
            add(saveAndCloseBt);
        }
        if (RXAccess.getInstance().canWrite(moDataObject)) {
            add(deleteBt);
        }
        add(closeBt);
    }
}